<template>
  <div>
      <div>
          <el-col :span="8">
            <el-form label-width="80px">
              <el-form-item label="商品编号">
                  <el-input v-model="userInfo.serialNumber"></el-input>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="8">
              <el-form label-width="80px">
                <el-form-item label="商品名称">
                    <el-input v-model="userInfo.productName"></el-input>
                </el-form-item>
              </el-form>
          </el-col>
          <el-col :span="8">
                <el-button type="primary" size="mini" @click="loadData" >查询</el-button>
                <el-button type="primary" size="mini" @click="add" >新增</el-button>
          </el-col>
      </div>
    <!-- 表格组件 -->
    <el-table :data="list" style="width: 100%;" @header-click="orderBy">
      <el-table-column prop="serialNumber"  label="商品编号" width="200">

      </el-table-column>
      <el-table-column prop="productName" label="商品类别" width="400"></el-table-column>
      <el-table-column prop="sellData" label="卖出时间" width="400"></el-table-column>
      <el-table-column label="操作">
        <!-- 模板插槽-->
        <template slot-scope="x">
          <el-button size="mini" type="primary" @click="update(x.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="del(x.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件-->
    <div>
      <el-pagination @size-change="selectRow" @current-change="curPage" :current-page="userInfo.page"
        :page-sizes="[1,2,3]" :page-size="userInfo.row" layout="sizes, prev, pager, next, jumper,total"
        :total="total">
      </el-pagination>
    </div>

  </div>
</template>

<script>
  import baseUrl from '../../static/js/BaseUrl.js';
  export default {
    name: "userInfo",
    data() {
      return {
        list: [], //当前页集合
        total: 0, //总条数
        totalPage: 0, //总页数
        sign: "编号↑", //默认升序
        title: "用户新增",
        dialogVisible: false, //控制对话框是否显示
        userInfo: { //分页对象
          page: 1,
          row: 4,
          id: undefined,
          serialNumber: undefined,
          productName: undefined,
          quantity: undefined,
          price: undefined,
          totalPrice: undefined,
          sellData: undefined,
          buyerPhone: undefined,
          buyerEmail: undefined,
          // certificateTime: undefined,

          orderBy: "asc", //默认升序
        }
      }
    },
    methods: {
      loadData() { //加载数据表格
        var self = this;
        $.ajax({
          url: baseUrl.url+"/merchandise-sales-record/list",
          type: "get",
          data: self.userInfo,
          dataType:"json",
          success: function(data) {
            console.log(data.data.list)
            self.list = data.data.list;
            self.total = data.data.total;
            self.totalPage = data.data.totalPage;
          }
        })
      },

      curPage(p) { //当前页触发事件
        this.userInfo.page = p;
        this.loadData();
      },
      selectRow(v) { //选中下来框触发事件
        this.userInfo.row = v;
        this.loadData();
      },
      orderBy(v) { //排序
          console.log(v);
       if (this.sign == "编号↑") {
          this.sign = "编号↓";
          this.userInfo.orderBy = "desc";

        } else {
          this.sign = "编号↑";
          this.userInfo.orderBy = "asc";
        }
        this.loadData();
      },
      add() { //打开新增模态框
        this.dialogVisible = true;
        //更改模态框标题
        this.title = "用户新增";
        this.clear();

      },
      clear() { //  清空
        //清空
        this.userInfo.goodsBatch = "";
        this.userInfo.warehouseAddress = "";
        this.userInfo.warehouseArea = "";
        this.userInfo.shelfNumber= "";
        this.userInfo.warehouseCategory = "";
        this.userInfo.remarks = "";
       // this.userInfo.supplierEmail = "";
       // this.userInfo.supplierPhone = "";
        //this.userInfo.supplierSex = "";

      },
      close() { //关闭模态框
        $("#myModal").modal("hide");
      },
      update(x) { //修改窗口
        this.dialogVisible = true;
        //更改模态框标题
        this.title = "用户修改";
        //数据回显
        this.userInfo.warehouseCode = x.warehouseCode;
        this.userInfo.warehouseAddress = x.warehouseAddress;
        this.userInfo.warehouseaArea = x.warehouseArea;
        this.userInfo.userInfo.shelfNumber = x.userInfo.shelfNumber;
        this.userInfo.warehouseCategory = x.warehouseCategory;
        this.userInfo.remarks = x.remarks;
       // this.userInfo.supplierPhone = x.supplierPhone;
       // this.userInfo.supplierSex = x.supplierSex;

      },
      del(x) { //删除
         var self = this;
         this.$confirm("你确定要删吗").then(function(){
            $.ajax({
              url: "http://localhost:8081/merchandise-sales-record/del",
              type: "get",
              data: {
                "id": x.warehouseCode
              },
              success: function(data) {
                self.$message(data.message);
                if (data.success) {
                  //刷新
                  self.loadData();
                }
              }
            })
        });


      },
      save() { // 保存

        var url = ""
        if (this.title == "用户新增") {
          url = "http://localhost:8081/merchandise-sales-record/add";
        } else {
          url = "http://localhost:8081/merchandise-sales-record/update";
        }

        var self = this;
        $.ajax({
          url: url,
          type: "get",
          data: this.userInfo,
          success: function(data) {
            self.$message(data.message);
            //self.$alert(data.message);
            if (data.success) {
              // 关闭模态框
              self.dialogVisible=false;
              //清空
              self.clear();
              //刷新
              self.loadData();
            }
          }
        })


      }


    },
    mounted() { //挂载函数baseUrl
      this.loadData();
      console.log(baseUrl);
    }
  }
</script>

<style>
</style>
